<?php

/**
 * Portal PSG - Polska Strefa Graffiti
 * 
 * Strona główna Portalu
 */
require_once '../_boot.php'; // import biblioteki rozruchowej

$_ApplicationHandle = RunApplication(APP_DOMAIN, APP_URLPATH, APP_PORT);
import('Psg.Core');
import('Psg.Auth');

/**
 * TRYB SERWISOWY - LOGOWANIE ADMINA
 * USUNIECIE PRZY VER ALPHA
 */
if (isset($_GET['service_mode'])) {
    $_SESSION['email'] = 'admin@psg.pl';
    $_SESSION['nick'] = 'admin';
} else if (isset($_GET['service_login'], $_GET['email'])) {
    if (Psg\Auth::userExists($_GET['email'])) {
        $_SESSION['email'] = $_GET['email'];
        $_SESSION['nick'] = \Psg\Core::getUserInfo($_GET['email'])['nick'];
    }
    Url::redirect();
}

/**
 * Sprawdzenie zalogowania użytkownika
 */
if (Psg\Auth::isLoggedIn()) {
    define('PSG_CURUSREMAIL', $_SESSION['email']);
    define('PSG_CURUSRENICK', $_SESSION['nick']);
    define('PSG_CURUSREUID', \Psg\Core::getUserIdByMail(PSG_CURUSREMAIL));
    define('PSG_CURUSRROLE', \Psg\Core::getUserRole(PSG_CURUSREMAIL));
    Psg\Core::lastActionUpdate(PSG_CURUSREMAIL);
    RunSite($_ApplicationHandle);
} else if (isset($_COOKIE['PSG_PERMLOGINTOKEN'], $_COOKIE['PSG_PERMLOGINUSER'])) {
    if (\Psg\Auth::permamentTokenExist($_COOKIE['PSG_PERMLOGINUSER'], $_COOKIE['PSG_PERMLOGINTOKEN'])) {
        $_SESSION['email'] = $_COOKIE['PSG_PERMLOGINUSER'];
        $_SESSION['nick'] = Psg\Core::getUserInfo($_COOKIE['PSG_PERMLOGINUSER'])['nick'];
    } else {
        setcookie('PSG_PERMLOGINUSER', '', time() - 3600);
        setcookie('PSG_PERMLOGINUSER', '', time() - 3600);
    }
    Url::redirect();
} else {
    Url::redirect('login.php', false);
}